package controllers;

import helpers.GoogleStorageHelper;
import helpers.IdUniqueHelper;
import helpers.StringHelper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.nio.channels.Channels;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileReadChannel;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileServiceFactory;


import entities.ApplicationConfig;
import entities.Article;
import entities.Category;

import quercus.CompileScriptEngine;
import user.models.ArticleModel;


public class DemoTemplateServlet extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String url = "http://site1.multiplearticle.com:8888/service/submit-article";
		
		try {
			String title = "article 3";
			String content = "content article 3";
			String category = "5G24GQIM";
			String name = "hoanbn";
			String email = "hoantocxoan@gmail.com";
			String website = "http://www.google.com";
			String keywords = "article3, keyword3";
			
			Connection conn = Jsoup.connect(url);
			Document doc = conn
				.header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
				.header("accept-Language", "en-us,en;q=0.5")
				//.header("host", "www.webpagetest.org")
				//.header("referer", "http://www.webpagetest.org/")
				//.cookie("o",response.cookie("o"))
				.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1")
				.data("title", title)
				.data("content", content)
				.data("category",category)
				.data("name",name)
				.data("email",email)
				.data("website",website)
				.data("keywords",keywords)
				.followRedirects(false)
				.post();
			
			response.getWriter().println(doc.html());
			
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
	}
	
	public static void main(String[] args) {
		/*String layoutPage = "<?php echo 'bbbb' ;  $abc->setName('yzfsdf'); ?>";
		ScriptEngineManager manager = new ScriptEngineManager();
	   ScriptEngine engine = manager.getEngineByName("php");
		Category cat = new Category();
		
		cat.setName("hahahaha");
		
		Bindings bindLayout = engine.getBindings(ScriptContext.ENGINE_SCOPE); 
		bindLayout.put("abc", cat);
		try {
			engine.eval(layoutPage);
			System.out.println(cat.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		*/
		
		/*String layoutPage = "<?php echo 'bbbb' ;  $abc->setName('yzfsdf'); ?>";
		
		CompiledScript compiledscript = CompileScriptEngine.getCompileScript("asdfasdfasdf", "layout", layoutPage);
		
		Category cat = new Category();
		
		cat.setName("hahahaha");
		
		Bindings bindLayout = compiledscript.getEngine().getBindings(ScriptContext.ENGINE_SCOPE);
		bindLayout.put("abc", cat);
		try {
			System.out.println(cat.getName());
			compiledscript.eval();
			System.out.println(cat.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		
		try {
			String fileName = "http://static.hoantocxoan.com/site1/ETS72VUGS0Q/archives.php";
			FileService fileService = FileServiceFactory.getFileService();
			
			AppEngineFile readableFile = new AppEngineFile(fileName);
			FileReadChannel readChannel = fileService.openReadChannel(
					readableFile, false);

			InputStream is = Channels.newInputStream(readChannel);
			int nRead;
			byte[] buffer = new byte[10240];
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
